<?php
namespace QnuiRings\Model;
// 软件名称：微折高级折扣创建
// 代码日期：v2.0 2017-07-28
// 作者：杨道泉 
// 文件名：UmpWeiZheModel.class.php
// 功能：UMP营销接口操作类
// 使用顺序：BuildMarketingTool、CreateActiveString、CreateActivity、CreateDetail

class UmpWeiZheRingModel{
	public $toolid = 110025001;
	public $actId;
	public $detailId;
	public $ToolString = '{ "toolSchema":"1.0.0","toolId":110025001,"name":"连环折扣普通工具","toolCode":"WeiZheRings","description":"连环折扣普通工具，验证标签，并且验证限购，并且订单满元或满件，减钱（允许限定倍数），打标签，删标签","providerKey":"21127968","type":"SUB_ORDER","privilege":"PRIVATE","orderType":"UNORDERABLE","startTime":"2017-07-09 00:00:00","endTime":"2099-12-31 23:59:59","operation":{ "schema":"1.0.0","type":"AND","elements":[{ "type":"DUAL","condition":{ "type":"AND","elements":[{ "type":"CONDITION","metaId":226,"params":[{ "paramId":"limitCount","kind":"UNDEFINE"}]},{ "type":"CONDITION","metaId":224,"params":[{ "paramId":"countAt","kind":"UNDEFINE"},{ "paramId":"enableMultiple","kind":"UNDEFINE"}]},{ "type":"CONDITION","metaId":219,"params":[{ "paramId":"amount","kind":"RESOURCE","value":{ "type":"RESOURCE","metaId":237,"params":[{ "paramId":"isBeforeDiscountCash","kind":"DEFINED","type":"Boolean","value":true}]}},{ "paramId":"amountAt","kind":"UNDEFINE"},{ "paramId":"priceMultiple","kind":"UNDEFINE"}]}]},"action":{ "type":"AND","elements":[{ "type":"CONDITIONAL","elements":[{ "type":"ACTION","metaId":202,"params":[],"tradeStatus":1}],"paramId":"calcDecrMultiple"},{ "type":"CONDITIONAL","elements":[{ "type":"ACTION","metaId":203,"params":[{ "paramId":"decreaseMoney","kind":"UNDEFINE"}],"tradeStatus":1}],"paramId":"decrease"},{ "type":"CONDITIONAL","elements":[{ "type":"ACTION","metaId":201,"params":[{ "paramId":"addTagId","kind":"UNDEFINE"}],"tradeStatus":3}],"paramId":"isAddTag"},{ "type":"CONDITIONAL","elements":[{ "type":"ACTION","metaId":208,"params":[{ "paramId":"removeTagId","kind":"UNDEFINE"}],"tradeStatus":3}],"paramId":"isRemoveTag"}]}},{ "type":"ACTION","metaId":213,"params":[],"tradeStatus":3}]}}';
	public function __construct(){
		Vendor('umpsdk.marketing.MarketingBuilder');
		Vendor('umpsdk.marketing.meta.MetaConstants');
		Vendor('umpsdk.marketing.meta.MetaData');
		Vendor('umpsdk.marketing.meta.ResourceDef');
		Vendor('umpsdk.marketing.meta.ConditionDef');
		Vendor('umpsdk.marketing.meta.ActionDef');
		Vendor('umpsdk.marketing.meta.TargetDef');
		Vendor('umpsdk.MarketingActivity');
		Vendor('umpsdk.MarketingDetail');
		Vendor('umpsdk.MarketingTool');
		Vendor('umpsdk.ParameterValue');
		Vendor('umpsdk.marketing.meta.ParameterDef');
		Vendor('umpsdk.marketing.internal.DualMetaData');
		Vendor('umpsdk.marketing.internal.LogicAndMetaData');
		Vendor('umpsdk.marketing.internal.LogicNotMetaData');
		Vendor('umpsdk.marketing.internal.LogicOrMetaData');
		Vendor('umpsdk.marketing.internal.ConditionalMetaData');
		Vendor('umpsdk.marketing.internal.MarketingActivityHandler');
		Vendor('umpsdk.marketing.internal.MarketingDetailHandler');
		Vendor('umpsdk.marketing.internal.MarketingToolHandler');
		$this->builder = new \MarketingBuilder();
		$this->c = new \TopClient;
		$this->c->appkey = C('APPKEY');
		$this->c->secretKey = C('APP_SECRET');
		$this->c->gatewayUrl = C("APIGATE");
	}
	public function loadActivityJson(){
		$tool = $this->builder->loadTool($this->ToolString);
		$activity = $this->builder->createActivity($tool);
		if($this->tag_id){
			$targetDef = new \TargetDef(240); //com.taobao.ump.meta.target.userTag  129测试 240正式
			$targetMeta = $this->builder->bind($targetDef, array($this->builder->newStringParameter('NEW_'.$this->tag_id)));
			$activity->setTarget($targetMeta);
		}
		$activity->setName($this->name);
		$activity->setDescription($this->description);
		$activity->setStartTime($this->start_time);
        $activity->setEndTime($this->end_time);
		return $this->builder->buildActivity($activity);
	}
	
	public function loadItemDetailJson(){
		$tool = $this->builder->loadTool($this->ToolString);
		$activityJson = $this->loadActivityJson();
		$activity = $this->builder->loadActivity($tool, $activityJson);
		$detail = $this->builder->createDetail($activity);
		$detail->define('limitCount', $this->itemSet["limitCount"], \ParameterDef::VALUE_TYPE_LONG);
		$detail->define('countAt', $this->itemSet["countAt"], \ParameterDef::VALUE_TYPE_LONG);
		$detail->define('enableMultiple', $this->itemSet["enableMultiple"], \ParameterDef::VALUE_TYPE_BOOLEAN);
		$detail->define('amountAt', $this->itemSet["amountAt"], \ParameterDef::VALUE_TYPE_LONG);
		$detail->define('priceMultiple', $this->itemSet["priceMultiple"], \ParameterDef::VALUE_TYPE_BOOLEAN);
		$detail->define('calcDecrMultiple', $this->itemSet["calcDecrMultiple"], \ParameterDef::VALUE_TYPE_BOOLEAN);
		$detail->define('decrease', $this->itemSet["decrease"], \ParameterDef::VALUE_TYPE_BOOLEAN);
		$detail->define('decreaseMoney', $this->itemSet["decreaseMoney"], \ParameterDef::VALUE_TYPE_LONG);
		$detail->define('isAddTag', $this->itemSet["isAddTag"], \ParameterDef::VALUE_TYPE_BOOLEAN);
		$detail->define('addTagId', 'NEW_'.$this->itemSet["addTagId"], \ParameterDef::VALUE_TYPE_STRING);
		$detail->define('isRemoveTag', $this->itemSet["isRemoveTag"], \ParameterDef::VALUE_TYPE_BOOLEAN);
		$detail->define('removeTagId', 'NEW_'.$this->itemSet["removeTagId"], \ParameterDef::VALUE_TYPE_STRING);
		$detail->setParticipateType("ITEM");
		$detail->setParticipateId($this->itemSet["itemId"]);
		return $this->builder->buildDetail($detail);
	}
	public function CreateItemDetail(){
		Vendor('taobaosdk/top/request/UmpDetailAddRequest');
		$req = new \UmpDetailAddRequest;
		$req->setActId($this->actId);
		$req->setContent($this->loadItemDetailJson());
		$resp = $this->c->execute($req, session('TOP_SESSION'));
		return $resp;
	}

	
	public function editItemDetail(){
		Vendor('taobaosdk/top/request/UmpDetailUpdateRequest');
		$req = new \UmpDetailUpdateRequest;
		$req->setDetailId($this->detailId);
		$req->setContent($this->loadItemDetailJson());
		$resp = $this->c->execute($req, session('TOP_SESSION'));
		$resp->detailJson = $this->loadItemDetailJson();
		return $resp;
	}
	public function CreateActivity(){
		Vendor('taobaosdk/top/request/UmpActivityAddRequest');
		$req = new \UmpActivityAddRequest;
		$req->setToolId($this->toolid);
		$req->setContent($this->loadActivityJson());
		$resp = $this->c->execute($req, session('TOP_SESSION'));
		return $resp;
	}
	public function editActivity(){
		Vendor('taobaosdk/top/request/UmpActivityUpdateRequest');
		$req = new \UmpActivityUpdateRequest;
		$req->setActId($this->actId);
		$req->setContent($this->loadActivityJson());
		$resp = $this->c->execute($req, session('TOP_SESSION'));
		return $resp;
	}
	public function removeActivity(){
		Vendor('taobaosdk/top/request/UmpActivityDeleteRequest');
		$req = new \UmpActivityDeleteRequest;
		$req->setActId($this->actId);
		$resp = $this->c->execute($req, session("TOP_SESSION"));
		return $resp;
	}
	public function getItemDetial(){
		Vendor('taobaosdk/top/request/UmpDetailsGetRequest');
		$req = new \UmpDetailsGetRequest;
		$req->setActId($this->actId);
		$req->setPageNo($this->page);
		$req->setPageSize($this->page_size);
		$resp = $this->c->execute($req, session("TOP_SESSION"));
		foreach($resp->contents->string as $k=>$str){
			$item=json_decode($str,true);
			if($item["status"]=="NORMAL"){
				$detail["activity_id"]=$this->actId;
				$detail["detail_id"]=$item['detailId'];
				$detail["item_id"]=$item["participateId"];
				foreach($item["operation"] as $opr){
					switch($opr["paramId"]){
						case "limitCount":
							$detail["limit_count"]=$opr['value'];
						break;
						case "decreaseMoney":
							$detail["promotion_value"]=$opr["value"];
						break;
						case "decrease":
							if($opr["value"]){
								$detail["promotion_type"]=0;
							}
						break;
					}
				}
				$common_item_detail[]=$detail;
			}
		}
		$ret = new \stdClass();
		$ret->code = (int)$resp->code;
		$ret->total_count = count($common_item_detail);
		$ret->detail_list->common_item_detail = $common_item_detail;
		return $ret;
	}
	public function removeDetail(){
		Vendor('taobaosdk/top/request/UmpDetailDeleteRequest');
		$req = new \UmpDetailDeleteRequest;
		$req->setDetailId($this->detailId);
		$resp = $this->c->execute($req, session("TOP_SESSION"));
		return $resp;
	}
}